package com.example.callservice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.alexd.jsonrpc.JSONRPCClient;
import org.alexd.jsonrpc.JSONRPCException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.example.object.ElectricPanels;
import com.example.object.Luminaires;
import com.example.object.Roads;
import com.example.ultil.Ultil;

public class LoginActivity extends Activity implements OnClickListener {

	String MENTHOD_LOGIN = "login" + "";
	String MENTHOD_PANELS = "panels";
	String MENTHOD_LUMINAIRES = "luminaires";
	String MENTHOD_DISTRICTS = "Districts";
	String MENTHOD_ROADS = "Roads";

	EditText txtUser;
	EditText txtPass;
	String KEY = "";
	String SV = "";

	ProgressDialog bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		findViewById(R.id.btnLogin).setOnClickListener(this);
		txtUser = (EditText) findViewById(R.id.txtuser);
		txtPass = (EditText) findViewById(R.id.txtpass);
		txtUser.setText("admin");
		txtPass.setText("123");
		findViewById(R.id.btnLogin).setFocusableInTouchMode(true);
		findViewById(R.id.btnLogin).requestFocus();
		// Create client specifying JSON-RPC version 2.0

	}


	public void call(){
		JSONRPCClient client = JSONRPCClient.create("http://service/uri", JSONRPCParams.Versions.VERSION_2);
		client.setConnectionTimeout(2000);
		client.setSoTimeout(2000);
		try 
		{
		  String string = client.callString("mymethod");
	//	  double d = client.callDouble("pow", x, y);
		  int i = client.callInt("add", 56, 25); 
		}
		catch (JSONRPCException e)
		{
		  e.printStackTrace();
		}
	}
	class BackgroundTask extends AsyncTask<String, Integer, Void> {
		@Override
		protected void onPreExecute() {
			bar = new ProgressDialog(LoginActivity.this);
			bar.setMessage("Vui lòng chờ tải dữ liệu..");
			bar.setIndeterminate(true);
			bar.show();

		}

		@Override
		protected Void doInBackground(String... params) {
			for (int i = 0; i < 4; i++) {
				System.out.println(i);
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					System.out.println(e);
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			bar.dismiss();
		}
	}

	private class LongRunningGetIO extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			String url = Ultil.ULR + MENTHOD_LOGIN + "?u="
					+ txtUser.getText().toString() + "&c=RHC&p="
					+ txtPass.getText().toString();
			String text = Ultil.callService(url);
			JSONObject jObj;
			try {
				jObj = new JSONObject(text);
				String key = jObj.getString("Key");
				Ultil.KEY = key.replace(" ", "+");
				String sv = jObj.getString("ServiceName");
				Ultil.SERVERNAME = sv.replace(" ", "+");
				new GetPanels().execute();
				new GetLuminiares().execute();
				new GetRoads().execute();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return text;
		}

	}

	private class GetPanels extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			String url = Ultil.ULR + MENTHOD_PANELS + "?k=" + Ultil.KEY
					+ "&sv=" + Ultil.SERVERNAME;
			String text = Ultil.callService(url);
			try {
				Ultil.lstElectricPanel = ElectricPanels.getListPanel(text);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Intent intent = new Intent(LoginActivity.this, GMap.class);
			startActivity(intent);
			return text;
		}

	}

	private class GetLuminiares extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			String url = Ultil.ULR + MENTHOD_LUMINAIRES + "?k=" + Ultil.KEY
					+ "&sv=" + Ultil.SERVERNAME;
			Log.e("URL _Luminaire", url);
			String text = Ultil.callService(url);
			try {
				Ultil.lstLuminaire = Luminaires.getListLumianire(text);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Intent intent = new Intent(LoginActivity.this, GMap.class);
			startActivity(intent);
			// Log.e("RS_Luminaire", text);
			return text;
		}

	}

	private class GetRoads extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			String url = Ultil.ULR + MENTHOD_ROADS + "?k=" + Ultil.KEY + "&sv="
					+ Ultil.SERVERNAME;
			Log.e("URL _PANEL", url);
			String text = Ultil.callService(url);
			try {
				Ultil.lstDistrict = Roads.getListRoad(text);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// Log.e("RS_PANEL", text);
			return text;
		}

		protected void onPostExecute(String results) {
			if (results != null) {
				Intent intent = new Intent(LoginActivity.this, GMap.class);
				startActivity(intent);
			}
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnLogin:
			new LongRunningGetIO().execute();
			new BackgroundTask().execute("Main");
			break;
		default:
			break;
		}
	}
}
